package com.ricchieri.manage.bean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.faces.bean.ManagedProperty;

import com.ricchieri.bean.CancelMultipleReservationBean;
import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.BasicFilter;
import com.ricchieri.filter.TripFilter;
import com.ricchieri.model.CancelationType;
import com.ricchieri.model.ReservationType;
import com.ricchieri.model.Trip;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.ICancelationTypeService;
import com.ricchieri.service.interf.IPassengerTypeService;
import com.ricchieri.service.interf.IReservationTypeService;
import com.ricchieri.service.interf.ITripService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

public abstract class CommonCancelMultipleReservationManageBean extends GenericManageBean {

	private static final long serialVersionUID = -3982979016657180990L;

	@ManagedProperty(value = "#{TripService}")
	private ITripService tripService;
	
	@ManagedProperty(value = "#{ReservationTypeService}")
	private IReservationTypeService reservationTypeService;
	
	@ManagedProperty(value = "#{PassengerTypeService}")
	private IPassengerTypeService passengerTypeService;
	
	@ManagedProperty(value = "#{CancelationTypeService}")
	private ICancelationTypeService cancelationTypeService;
	
	private User selectedUser;
	private CancelMultipleReservationBean cancelMultipleReservationBean;

	public ICancelationTypeService getCancelationTypeService() {
		return cancelationTypeService;
	}

	public void setCancelationTypeService(
			ICancelationTypeService cancelationTypeService) {
		this.cancelationTypeService = cancelationTypeService;
	}
	
	public ITripService getTripService() {
		return tripService;
	}

	public void setTripService(ITripService tripService) {
		this.tripService = tripService;
	}

	public IReservationTypeService getReservationTypeService() {
		return reservationTypeService;
	}

	public void setReservationTypeService(
			IReservationTypeService reservationTypeService) {
		this.reservationTypeService = reservationTypeService;
	}
	
	public User getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}

	public IPassengerTypeService getPassengerTypeService() {
		return passengerTypeService;
	}

	public void setPassengerTypeService(IPassengerTypeService passengerTypeService) {
		this.passengerTypeService = passengerTypeService;
	}

	public CancelMultipleReservationBean getCancelMultipleReservationBean() {
		if(cancelMultipleReservationBean == null) {
			cancelMultipleReservationBean = new CancelMultipleReservationBean();
		}
		return cancelMultipleReservationBean;
	}

	public void setCancelMultipleReservationBean(CancelMultipleReservationBean cancelMultipleReservationBean) {
		this.cancelMultipleReservationBean = cancelMultipleReservationBean;
	}

	public Map<String, String> getDaysWeek() {
		
		Map<String, String> days = new LinkedHashMap<String, String>();
		days.put(FacesUtil.getMessage("sunday"), BusinessSettings.DAY_SUNDAY);
		days.put(FacesUtil.getMessage("monday"), BusinessSettings.DAY_MONDAY);
		days.put(FacesUtil.getMessage("tuesday"), BusinessSettings.DAY_TUESDAY);
		days.put(FacesUtil.getMessage("wednesday"), BusinessSettings.DAY_WEDNESDAY);
		days.put(FacesUtil.getMessage("thursday"), BusinessSettings.DAY_THURSDAY);
		days.put(FacesUtil.getMessage("friday"), BusinessSettings.DAY_FRIDAY);
		days.put(FacesUtil.getMessage("saturday"), BusinessSettings.DAY_SATURDAY);
		
		return days;
	}

	public ReservationType obtainReservationType() {
		if(getCancelMultipleReservationBean() != null && StringUtils.isNotEmpty(getCancelMultipleReservationBean().getSelectedReservationType())) {
			BasicFilter reservationTypeFilter = new BasicFilter();
			reservationTypeFilter.setId(Integer.valueOf(getCancelMultipleReservationBean().getSelectedReservationType()));
			return getReservationTypeService().getReservationTypeById(reservationTypeFilter);
		} else {
			return null;
		}
	}

	public Trip obtainTrip() {
		if(getCancelMultipleReservationBean() != null && StringUtils.isNotEmpty(getCancelMultipleReservationBean().getSelectedTrip())) {
			TripFilter tripFilter = new TripFilter();
			tripFilter.setId(Integer.valueOf(getCancelMultipleReservationBean().getSelectedTrip()));
			return getTripService().getTripById(tripFilter);
		} else {
			return null;
		}
	}

	public CancelationType obtainCancelationType() {
		if(getCancelMultipleReservationBean() != null && StringUtils.isNotEmpty(getCancelMultipleReservationBean().getSelectedCancelationType())) {
			BasicFilter cancelationTypeFilter = new BasicFilter();
			cancelationTypeFilter.setId(Integer.valueOf(getCancelMultipleReservationBean().getSelectedCancelationType()));
			return getCancelationTypeService().getCancelationById(cancelationTypeFilter);
		} else {
			return null;
		}
	}

	public String obtainCancelDescription(String key) {
		if(StringUtils.isNotEmpty(key)) {
			return this.obtainDescription(key);
		} else {
			return this.obtainDescription("all");
		}
	}

	public String formatTime(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("kk:mm:ss");
		
		if(date != null) {
			return format.format(date);
		}
		return this.obtainDescription("all");
	}

	public String obtainTripDescription() {
		Trip trip = obtainTrip();
		if(trip != null) {
			return trip.getFormatTrip();
		}
		return this.obtainDescription("all");
	}

}